package com.shenmazong.servertalkseller.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.shenmazong.pojo.TbSeller;
import com.shenmazong.pojo.TbSellerFeeRecord;
import com.shenmazong.pojo.TbStudent;
import com.shenmazong.servertalkseller.mapper.TbSellerFeeRecordMapper;
import com.shenmazong.servertalkseller.mapper.TbSellerMapper;
import com.shenmazong.servertalkseller.mapper.TbStudentMapper;
import com.shenmazong.servertalkseller.service.TbSellerService;
import com.shenmazong.utils.JwtTokenUtils;
import com.shenmazong.utils.ResponseResult;
import com.shenmazong.vo.SellerVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class TbSellerServiceImpl implements TbSellerService {
    @Autowired
    private TbSellerMapper tbSellerMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TbSellerFeeRecordMapper tbSellerFeeRecordMapper;

    @Autowired
    private TbStudentMapper tbStudentMapper;

    @Override
    public ResponseResult setSellerPass(Integer userId,String oldPass,String newPass) {
        ResponseResult result=ResponseResult.SUCCESS();
        //判断销售是否存在
        TbSeller seller = tbSellerMapper.selectById(userId);
        if (seller==null){
            result.setFailMessage("销售不存在");
            return  result;
        }

        String oldPassWord=oldPass+"."+seller.getPassSalt();
        //加密
        String oldMd5Password= DigestUtils.md5DigestAsHex(oldPassWord.getBytes());

        //判断旧密码是否正确
        if (!seller.getUserPass().equals(oldMd5Password)) {
            result.setFailMessage("密码不正确");
            return  result;
        }
        //判断是否删除
        if (!seller.getStatus().equals(0)){
            result.setFailMessage("销售已经删除");
            return  result;
        }
        //加盐
        String password=newPass+"."+seller.getPassSalt();
        //加密
        String md5Password= DigestUtils.md5DigestAsHex(password.getBytes());
        seller.setUserPass(md5Password);
        log.info("pass:"+md5Password);
        //修改密码
        tbSellerMapper.updateById(seller);
        seller.setUserPass("");
        seller.setPassSalt("");
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("seller",seller);
        result.setResult(hashMap);
        return result;
    }

    @Override
    public ResponseResult doSellerLogin(String userName, String passWord) {
        ResponseResult result=ResponseResult.SUCCESS();
        HashMap<String, Object> hashMap = new HashMap<>();
        TbSeller seller = tbSellerMapper.selectOne(new QueryWrapper<TbSeller>()
                .eq("user_name", userName).eq("status", 0).last("limit 1")
        );
        //判断用户是否存在
        if (seller==null){
            result.setFailMessage("销售不存在");
            return  result;
        }
        //加盐
        String password=passWord+"."+seller.getPassSalt();
        //加密
        String md5Password= DigestUtils.md5DigestAsHex(password.getBytes());
        //判断密码是否相同
        if (!seller.getUserPass().equals(md5Password)) {
            result.setFailMessage("密码不正确");
            return  result;
        }
        //返回值中将盐和密码设为空
        seller.setUserPass("");
        seller.setPassSalt("");
        //生成token
        String token= JwtTokenUtils.makeToken(seller.getId(),seller.getUserName(),"seller");
        //存取数据
        hashMap.put("token",token);
        hashMap.put("seller",seller);
        result.setResult(hashMap);

        //redis单点登陆
        String key="seller_token_"+seller.getId();
        redisTemplate.opsForValue().set(key,token,1, TimeUnit.DAYS);
        return  result;
    }

    @Override
    public ResponseResult getFinishClassByPage(Integer userId, Integer currentPage, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();
        TbSeller tbSeller = tbSellerMapper.selectById(userId);
        //判断销售是否存在
        if (tbSeller==null){
            result.setFailMessage("销售不存在");
            return result;
        }
        //分页查询
        Page<SellerVo> page = new Page<>(currentPage,pageSize);
        IPage<SellerVo> iPage = tbSellerMapper.getFinishClassByPage(page, userId);
        Map<String,Object> map = new HashMap<>();
        map.put("sellers", iPage);
        result.setResult(map);
        return result;
    }

    @Override
    public ResponseResult getWillClassByPage(Integer userId, Integer currentPage, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();
        TbSeller tbSeller = tbSellerMapper.selectById(userId);
        //判断销售是否存在
        if (tbSeller==null){
            result.setFailMessage("销售不存在");
            return result;
        }
        //分页查询
        Page<SellerVo> page=new Page<>(currentPage,pageSize);
        IPage<SellerVo> iPage=tbSellerMapper.getWillClassByPage(page,userId);
        Map<String,Object> map=new HashMap<>();
        map.put("sellers",iPage);
        result.setResult(map);
        return result;
    }

    @Override
    public ResponseResult getStudentByPage(Integer userId, Integer currentPage, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();
        TbSeller tbSeller = tbSellerMapper.selectById(userId);
        //判断销售是否存在
        if (tbSeller==null){
            result.setFailMessage("销售不存在");
            return result;
        }
        //分页查询
        Page<TbStudent> page = new Page<>(currentPage,pageSize);
        IPage<TbStudent> iPage = tbStudentMapper.selectPage(page, new QueryWrapper<TbStudent>().eq("seller_id", userId));
        Map<String,Object> map = new HashMap<>();
        map.put("students", iPage);
        result.setResult(map);
        return result;
    }

    @Override
    public ResponseResult getClassFeeRecordByPage(Integer userId, Integer currentPage, Integer pageSize) {
        ResponseResult result=ResponseResult.SUCCESS();
        TbSellerFeeRecord tbSellerFeeRecord = tbSellerFeeRecordMapper.selectOne(
                new QueryWrapper<TbSellerFeeRecord>().eq("seller_id",userId).last("limit 1")
        );
        //判断明细是否存在
        if (tbSellerFeeRecord == null){
            result.setFailMessage("提成明细不存在");
            return result;
        }

        //分页查询
        Page<TbSellerFeeRecord> page = new Page<>(currentPage,pageSize);
        IPage<TbSellerFeeRecord> iPage = tbSellerMapper.getClassFeeRecordByPage(page, userId);
        Map<String,Object> map = new HashMap<>();
        map.put("sellerRecord", iPage);
        result.setResult(map);
        return result;
    }


}
